package com.cn.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.model.system.SysPost;
import com.cn.model.vo.SysPostQueryVo;
import com.cn.system.mapper.SysPostMapper;
import com.cn.system.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {
    @Autowired
    private SysPostMapper sysPostMapper;

    //分页
    @Override
    public IPage<SysPost> selectPage(Page<SysPost> pageParam, SysPostQueryVo postQueryVo) {
        String name = postQueryVo.getName();
        String postCode = postQueryVo.getPostCode();
        Boolean status = postQueryVo.getStatus();
        QueryWrapper<SysPost> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("name", name);
        }
        if (!StringUtils.isEmpty(postCode)){
            wrapper.like("post_code", postCode);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status", status);
        }
        IPage<SysPost> pageModel = sysPostMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

    //更新状态
    @Override
    public void updateStatus(Long id, Integer status) {
        SysPost sysPost = sysPostMapper.selectById(id);
        sysPost.setStatus(status);
        sysPostMapper.updateById(sysPost);
    }
}
